import {
  utils,
  sharedStore,
  sessionStore
} from "./utils.js";

const $_debug = true;

const names = [
  'mode', // string: name of a mode
  'projection_type', // string: name of a supported projection
  'orientation', // {rotate: number[2], scale: number}

  'time_cursor', // string: "current" or {year:, month:, day:, hour:, minute:}, always UTC
  'z_level', // string: name of a level
  'animation_type', // string: name of a supported animation type
  'overlay_type', // string: name of an overlay

  'spotlight_coords', // [λ, φ] coordinates of spotlight marker or undefined
  'show_grid_points', // boolean
  'animation_enabled', // boolean
]

const disable = false;
const leaves = true;
const cmd = true;
const href = true;
const link = true;
const isSessionStore = true;

var overlay_item = {
  name: 'overlay_type',
  key: 'overlay_type',
  icon: 'overlay_type',
  title: '背景',
  leaves,
  fn: utils.setOverlayType
};

var mode_items = [{
    name: 'air',
    key: 'air',
    icon: 'el-icon-setting',
    title: '大气',
    cmd,
    disable
  },
  {
    name: 'ocean',
    key: 'ocean',
    icon: 'el-icon-setting',
    title: '海洋',
    cmd,
    disable
  },
  {
    name: 'chem',
    key: 'chem',
    icon: 'el-icon-setting',
    title: '化学污染物',
    cmd,
    disable
  },
  {
    name: 'particulates',
    key: 'particulates',
    icon: 'el-icon-setting',
    title: '颗粒物',
    cmd,
    disable
  },
  {
    name: 'space',
    key: "space",
    icon: 'el-icon-printer',
    title: '空间',
    cmd,
    disable
  },
]

var z_level_items = [{
  name: 'surface',
  key: 'surface',
  icon: 'el-icon-setting',
  title: '近地面',
  cmd,
  disable
}, {
  name: '1000hPa',
  key: '1000hPa',
  icon: 'el-icon-setting',
  title: '1000hPa',
  cmd,
  disable
}, {
  name: '850hPa',
  key: '850hPa',
  icon: 'el-icon-setting',
  title: '850hPa',
  cmd,
  disable
}, {
  name: '700hPa',
  key: '700hPa',
  icon: 'el-icon-setting',
  title: '700hPa',
  cmd,
  disable
}, {
  name: '500hPa',
  key: '500hPa',
  icon: 'el-icon-setting',
  title: '500hPa',
  cmd,
  disable
}, {
  name: '250hPa',
  key: '250hPa',
  icon: 'el-icon-setting',
  title: '250hPa',
  cmd,
  disable
}, {
  name: '70hPa',
  key: '70hPa',
  icon: 'el-icon-setting',
  title: '70hPa',
  cmd,
  disable
}, {
  name: '10hPa',
  key: '10hPa',
  icon: 'el-icon-setting',
  title: '10hPa',
  cmd,
  disable
}]

var animation_type_items = [{
    id: 'wind_field',
    name: 'wind',
    key: 'wind',
    icon: 'el-icon-setting',
    title: '风场',
    cmd,
    disable
  },
  {
    name: 'currents',
    key: 'currents',
    icon: 'el-icon-setting',
    title: '洋流',
    cmd,
    disable
  },
  {
    name: 'primary_waves',
    key: 'primary_waves',
    icon: 'el-icon-setting',
    title: '海浪',
    cmd,
    disable
  },
  {
    name: 'animation_enabled',
    key: false,
    icon: 'el-icon-setting',
    title: '开启动画',
    cmd,
    fn: utils.toggleAnimationEnabled,
    disable
  }
]

var projection_type_items = [{
    name: 'orthographic',
    key: 'orthographic',
    icon: 'el-icon-setting',
    title: '正射投影',
    cmd,
    disable
  },
  {
    name: 'equirectangular',
    key: 'equirectangular',
    icon: 'el-icon-setting',
    title: '等距圆柱投影',
    cmd,
    disable
  },
  {
    name: 'patterson',
    key: 'patterson',
    icon: 'el-icon-setting',
    title: '帕特森投影',
    cmd,
    disable
  },
  {
    name: 'winkel3',
    key: 'winkel3',
    icon: 'el-icon-setting',
    title: '温克尔三投影',
    cmd,
    disable
  },
  {
    name: 'conic_equidistant',
    key: 'conic_equidistant',
    icon: 'el-icon-printer',
    title: '等距圆锥投影',
    cmd,
    disable
  },
  {
    name: 'atlantis',
    key: 'atlantis',
    icon: 'el-icon-cold-drink',
    title: '亚特兰蒂斯投影',
    cmd,
    disable
  },
  {
    name: 'stereographic',
    key: 'stereographic',
    icon: 'el-icon-setting',
    title: '立体投影',
    cmd,
    disable
  },
  {
    name: 'waterman',
    key: 'waterman',
    icon: 'el-icon-setting',
    title: '沃特曼蝴蝶投影',
    cmd,
    disable
  }
]

var more_items = [{
    name: 'hd_enabled',
    key: false,
    isSessionStore,
    icon: 'el-icon-setting',
    title: '高精度',
    cmd,
    fn: utils.toggleHD,
    disable
  },
  {
    name: 'show_grid_points',
    key: false,
    icon: 'el-icon-setting',
    title: '网格点',
    cmd,
    fn: utils.toggleGrid,
    disable
  },
]

var $items = [{
    name: 'mode',
    key: 'mode',
    icon: 'mode',
    title: '模式',
    leaves,
    fn: utils.setMode,
    children: mode_items
  },
  {
    'for': "air",
    name: 'z_level',
    key: 'z_level',
    icon: 'z_level',
    title: '高度',
    leaves,
    fn: utils.setZLevel,
    children: z_level_items
  },
  {
    name: 'animation_type',
    key: 'animation_type',
    icon: 'animation_type',
    title: '动画',
    leaves,
    fn: utils.setAnimation,
    children: animation_type_items
  },
  overlay_item,
  {
    name: 'projection_type',
    key: 'projection_type',
    icon: 'projection',
    title: '投影',
    leaves,
    fn: utils.setProjection,
    children: projection_type_items
  },
  {
    name: 'more',
    key: 'more',
    icon: 'more',
    title: '更多',
    leaves,
    children: more_items
  },
];

const air_overlays = [{
    id: "wind_speed",
    name: 'wind',
    key: 'wind',
    icon: 'el-icon-setting',
    title: '风速',
    cmd,
    disable
  },
  {
    id: "temp",
    name: 'temp',
    key: 'temp',
    icon: 'el-icon-setting',
    title: '温度',
    cmd,
    disable
  },
  {
    name: 'relative_humidity',
    key: 'relative_humidity',
    icon: 'el-icon-setting',
    title: '相对湿度',
    cmd,
    disable
  },
  {
    name: 'wind_power_density',
    key: 'wind_power_density',
    icon: 'el-icon-setting',
    title: '瞬时风电功率密度',
    cmd,
    disable
  },
  {
    name: 'precip_3hr',
    key: 'precip_3hr',
    icon: 'el-icon-setting',
    title: '3小时累积降水量',
    cmd,
    disable
  },
  {
    name: 'cape',
    key: 'cape',
    icon: 'el-icon-setting',
    title: '对流可用位能（表面）',
    cmd,
    disable
  },
  {
    name: 'total_precipitable_water',
    key: 'total_precipitable_water',
    icon: 'el-icon-setting',
    title: '水汽含量',
    cmd,
    disable
  },
  {
    name: 'total_cloud_water',
    key: 'total_cloud_water',
    icon: 'el-icon-setting',
    title: '云中所含水汽总和',
    cmd,
    disable
  },
  {
    name: 'mean_sea_level_pressure',
    key: 'mean_sea_level_pressure',
    icon: 'el-icon-setting',
    title: '平均海平面压力',
    cmd,
    disable
  },
  {
    name: 'misery_index',
    key: 'misery_index',
    icon: 'el-icon-setting',
    title: '体感温度',
    cmd,
    disable
  },
  {
    name: 'none',
    key: 'none',
    icon: 'el-icon-setting',
    title: '无',
    cmd,
    disable
  },
];

const ocean_overlays = [{
    name: 'currents',
    key: 'currents',
    icon: 'el-icon-setting',
    title: '洋流',
    cmd,
    disable
  },
  {
    name: 'primary_waves',
    key: 'primary_waves',
    icon: 'el-icon-setting',
    title: '波峰周期',
    cmd,
    disable
  },
  {
    name: 'significant_wave_height',
    key: 'significant_wave_height',
    icon: 'el-icon-setting',
    title: '有效浪高',
    cmd,
    disable
  },
  {
    name: 'sea_surface_temp',
    key: 'sea_surface_temp',
    icon: 'el-icon-setting',
    title: '海面温度',
    cmd,
    disable
  },
  {
    name: 'sea_surface_temp_anomaly',
    key: 'sea_surface_temp_anomaly',
    icon: 'el-icon-setting',
    title: '海温偏差值',
    cmd,
    disable
  },
  {
    name: 'none',
    key: 'none',
    icon: 'el-icon-setting',
    title: '无',
    cmd,
    disable
  }
]
const chem_overlays = [{
    name: 'cosc',
    key: 'cosc',
    icon: 'el-icon-setting',
    title: '地表一氧化碳浓度',
    cmd,
    disable
  },
  {
    name: 'co2sc',
    key: 'co2sc',
    icon: 'el-icon-setting',
    title: '地表二氧化碳浓度',
    cmd,
    disable
  },
  {
    name: 'so2smass',
    key: 'so2smass',
    icon: 'el-icon-setting',
    title: '地表二氧化硫总质量',
    cmd,
    disable
  },
  {
    name: 'no2',
    key: 'no2',
    icon: 'el-icon-setting',
    title: '二氧化氮',
    cmd,
    disable
  },
  {
    name: 'none',
    key: 'none',
    icon: 'el-icon-setting',
    title: '无',
    cmd,
    disable
  }
]
const particulates_overlays = [{
    name: 'duexttau',
    key: 'duexttau',
    icon: 'el-icon-setting',
    title: '尘埃消光（气溶胶光学厚度，550纳米）',
    cmd,
    disable
  },
  {
    name: 'pm1',
    key: 'pm1',
    icon: 'el-icon-setting',
    title: 'PM1',
    cmd,
    disable
  },
  {
    name: 'pm2.5',
    key: 'pm2.5',
    icon: 'el-icon-setting',
    title: 'PM2.5',
    cmd,
    disable
  },
  {
    name: 'pm10',
    key: 'pm10',
    icon: 'el-icon-setting',
    title: 'PM10',
    cmd,
    disable
  },
  {
    name: 'suexttau',
    key: 'suexttau',
    icon: 'el-icon-setting',
    title: '硫酸盐消光（气溶胶光学厚度，550纳米）',
    cmd,
    disable
  },
  {
    name: 'none',
    key: 'none',
    icon: 'el-icon-setting',
    title: '无',
    cmd,
    disable
  }
]

const space_overlays = [{
    id: "aurora",
    name: 'aurora',
    key: 'aurora',
    icon: 'el-icon-setting',
    title: '极光',
    cmd,
    disable
  },
  {
    id: "temp",
    name: 'none',
    key: 'none',
    icon: 'el-icon-setting',
    title: '无',
    cmd,
    disable
  }
]

var all_items = [...mode_items, ...z_level_items, ...animation_type_items, ...projection_type_items, ...more_items, ...
  air_overlays, ...ocean_overlays, ...chem_overlays, ...particulates_overlays, ...space_overlays
]

var all_items_map = {};
all_items.forEach(e => {
  all_items_map[e.id || e.name] = e;
})

function findItemPath(name) {
  if (!name) return;
  for (let i = 0; i < $items.length; ++i) {
    const e = $items[i]
    if (name === e.name) {
      return [name];
    }
    if (e.children && e.children.length) {
      for (let j = 0; j < e.children.length; ++i) {
        const x = e.children[j]
        if (name === x.name) {
          return [e.name, x.name];
        }
      }
    }
  }
  return null
}

function findItem(name) {
  if (!name) return;
  for (let i = 0; i < $items.length; ++i) {
    const e = $items[i]
    if (name === e.name) {
      return e;
    }
    if (e.children && e.children.length) {
      for (let j = 0; j < e.children.length; ++j) {
        const x = e.children[j]
        if (name === x.name) {
          return x;
        }
      }
    }
  }
  return null
}

function getItems(opts) {
  let items = $items;
  overlay_item.children = undefined;
  if (opts.mode !== "air") {
    items = $items.filter(e => e['for'] !== 'air')
  }
  if (opts.mode === "air") {
    overlay_item.children = air_overlays;
  } else if (opts.mode === "ocean") {
    overlay_item.children = ocean_overlays;
  } else if (opts.mode === "chem") {
    overlay_item.children = chem_overlays;
  } else if (opts.mode === "particulates") {
    overlay_item.children = particulates_overlays;
  } else if (opts.mode === "space") {
    overlay_item.children = space_overlays;
  }

  console.log('opts', opts);
  
  const keys = Object.keys(opts)
  items.forEach(e => {
    e.active = false;
    const val = opts[e.name]
    if (e.children) {
      for (let i = 0; i < e.children.length; ++i) {
        let x = e.children[i]
        x.active = (x.key === val)
        if (["animation_enabled", "show_grid_points", "hd_enabled"].includes(x.name)) {
          x.active = opts[x.name]
        }
      }
    }
  })
  return items
}

/*
animation_enabled: true
animation_type: "wind"
colorbar_cursor: undefined
display_timezone: "device"
error: undefined
font_size: "medium"
hd_enabled: false
interpolation_type: "bilinear"
mode: "air"
notice: undefined
orientation:
rotate: Array(2)
0: 253.56
1: -36.64
length: 2
__proto__: Array(0)
scale: 1130
overlay_type: "wind"
progress: undefined
projection_type: "orthographic"
selected_units: undefined
show_cta: false
show_grid_points: false
show_menu: false
spotlight_coords: Array(2)
0: 67.733
1: 53.331
length: 2
__proto__: Array(0)
status: undefined
time_cursor: "current"
topology_url: "./earth-topo.c87af6.json"
z_level: "surface"
*/

function getItemTitle(name, attrs) {
  const item = all_items_map[name];
  return item?.title
}

function getTitle(attrs) {
  $_debug && console.log('attrs', attrs);
  let animation_type = attrs.animation_type;
  if (animation_type === "wind") animation_type = "wind_field"
  let overlay_type = attrs.overlay_type;
  if (overlay_type === "wind") overlay_type = "wind_speed"
  // const name = attrs.mode
  let result = `${getItemTitle(attrs.mode)||""}`;
  if (attrs.mode === 'air') {
    (result = `${result} ${getItemTitle(attrs.z_level,attrs)||""}${getItemTitle(animation_type)||""}`);
  } else {
    (result = `${result} ${getItemTitle(animation_type)||""}`);
  }
  result = `${result} 
  ${getItemTitle(overlay_type)||""}
  ${getItemTitle(attrs.projection_type)||""}
  `;
  return result
}
// 'time_cursor', // string: "current" or {year:, month:, day:, hour:, minute:}, always UTC
function getTimeCursor(attrs) {
  let tc = attrs.time_cursor
  if (tc === "current") {
    return "现在"
  }
  return tc ? `${tc.year}-${tc.month}-${tc.day} ${("00"+tc.hour).slice(-2)}` : ""
}

export {
  all_items_map,
  findItem,
  getItems,
  getTitle,
  getTimeCursor,
  sharedStore,
  sessionStore
}
